<!-- Login.vue -->
<template>
  <div class="mid-class">
    <div class="art-right-w3ls">
      <h2>请登录</h2>
      <form ref="formRef" @submit.prevent="handleLogin">
        <div class="main">
          <div class="form-left-to-w3l">
            <input
              v-model="formData.account"
              type="text"
              name="name"
              placeholder="账号"
              required=""
            />
          </div>
          <div class="form-left-to-w3l password-container">
            <input
              v-model="formData.password"
              :type="passwordVisible ? 'text' : 'password'"
              name="password"
              placeholder="密码"
              required=""
            />
            <span class="password-toggle" @click="togglePasswordVisibility">
              <el-icon>
                <component :is="passwordVisible ? 'View' : 'Hide'" />
              </el-icon>
            </span>
            <div class="clear"></div>
          </div>
        </div>
        <div class="left-side-forget">
          <input type="checkbox" class="checked" v-model="rememberMe" />
          <span class="remenber-me">记住我 </span>
        </div>
        <div class="clear"></div>
        <div class="btnn">
          <button type="submit" :disabled="loading">
            {{ loading ? "登录中..." : "登录" }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { View, Hide } from "@element-plus/icons-vue"; // 导入所需图标
import store from "@/store";
import { useRoute } from "vue-router";
import router from "@/router";

const formRef = ref(null);
const rememberMe = ref(false);
const passwordVisible = ref(false);

const formData = reactive({
  account: "",
  password: "",
});

const rules = reactive({
  account: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { pattern: /^\d+$/, message: "用户名必须为纯数字", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, message: "密码长度不能少于6位", trigger: "blur" },
  ],
});

const loading = ref(false);
const route = useRoute();
const redirect = ref(route.query?.redirect || "/");

// 页面加载时，尝试从本地存储获取保存的账号密码
onMounted(() => {
  const savedCredentials = localStorage.getItem("userCredentials");
  if (savedCredentials) {
    try {
      const credentials = JSON.parse(savedCredentials);
      formData.account = credentials.account;
      formData.password = credentials.password;
      rememberMe.value = true;
    } catch (e) {
      console.error("无法解析保存的凭证", e);
      localStorage.removeItem("userCredentials");
    }
  }
});

const handleLogin = async () => {
  if (!formRef.value) return;

  try {
    loading.value = true;
    await store.dispatch("user/login", formData);

    // 如果勾选了"记住我"，则保存凭证到本地存储
    if (rememberMe.value) {
      localStorage.setItem(
        "userCredentials",
        JSON.stringify({
          account: formData.account,
          password: formData.password,
        })
      );
    } else {
      // 未勾选"记住我"，则清除本地存储的凭证
      localStorage.removeItem("userCredentials");
    }

    router.push({ path: redirect.value || "/" });
  } catch (error) {
    console.error(error);
  } finally {
    loading.value = false;
  }
};

// 切换密码可见性
const togglePasswordVisibility = () => {
  passwordVisible.value = !passwordVisible.value;
};
</script>

<style scoped>
/* float right */

.float-lt {
  float: left;
}

/* float left */

.clear {
  clear: both;
}

/* horizontal menu */

img {
  max-width: 100%;
}

/*end reset*/

html,
body {
  padding: 0;
  margin: 0;
  background: #fff;
  font-family: "Source Sans Pro", sans-serif;
}

body a {
  transition: 0.5s all;
  -webkit-transition: 0.5s all;
  -moz-transition: 0.5s all;
  -o-transition: 0.5s all;
  -ms-transition: 0.5s all;
  text-decoration: none;
}

body a:hover {
  text-decoration: none;
}

body a:focus,
a:hover {
  text-decoration: none;
}

select,
input[type="email"],
input[type="text"],
input[type="password"],
input[type="submit"],
textarea {
  font-family: "Source Sans Pro", sans-serif;
  transition: 0.5s all;
  -webkit-transition: 0.5s all;
  -moz-transition: 0.5s all;
  -o-transition: 0.5s all;
  -ms-transition: 0.5s all;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  margin: 0;
  padding: 0;
  font-family: "Source Sans Pro", sans-serif;
  letter-spacing: 1px;
}

p {
  margin: 0;
  padding: 0;
  letter-spacing: 1px;
  font-family: "Source Sans Pro", sans-serif;
}

ul {
  margin: 0;
  padding: 0;
}

/*-- //Reset-Code --*/

/*--background --*/

.mid-class {
  /* position: relative;*/
  background: url("@/assets/VCG41N1371039912.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  box-sizing: border-box;
  min-height: 100vh;
}

.mid-class {
  display: -webkit-flex;
  display: -webkit-box;
  display: -moz-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: space-between;
  justify-content: space-between;
}

.art-right-w3ls {
  flex-basis: 60%;
  -webkit-flex-basis: 60%;
  box-sizing: border-box;
}

.form-left-to-w3l input[type="email"],
.form-left-to-w3l input[type="password"],
.form-left-to-w3l input[type="text"] {
  width: 80%;
  color: #000;
  outline: none;
  border: none;
  border-bottom: #00a1ff 2px solid;
  -webkit-appearance: none;
  background: #ffffff96;
  -moz-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  -ms-transition: all 0.5s ease-in-out;
  box-sizing: border-box;
}

::-webkit-input-placeholder {
  /* Chrome/Opera/Safari */
  color: #000;
}

::-moz-placeholder {
  /* Firefox 19+ */
  color: #000;
}

:-ms-input-placeholder {
  /* IE 10+ */
  color: #000;
}

:-moz-placeholder {
  /* Firefox 18- */
  color: #000;
}

.art-right-w3ls h2 {
  font-weight: 500;
  color: #ffffff;
}

button[type="submit"] {
  background: #00a1ff;
  outline: none;
  color: #fff;
  font-size: 14px;
  font-weight: 400;
  border: none;
  padding: 11px 20px;
  letter-spacing: 1px;
  text-transform: capitalize;
  cursor: pointer;
  transition: 0.5s all;
  margin: 56px 0px 0px;
  -webkit-transition: 0.5s all;
  -o-transition: 0.5s all;
  -moz-transition: 0.5s all;
  -ms-transition: 0.5s all;
}

button[type="submit"]:hover {
  opacity: 0.8;
}

.remenber-me,
a.for {
  font-size: 14px;
  color: #fff;
  letter-spacing: 1px;
  text-transform: capitalize;
}

.remenber-me {
  padding-left: 22px;
}

.left-side-forget {
  float: left;
  position: relative;
}

input.checked {
  display: inline-block;
  position: absolute;
  top: 5px;
  margin: 0px;
  cursor: pointer;
}

/*--//background --*/

/*--responsive--*/

@media (max-width: 1920px) {
  .form-left-to-w3l input[type="email"],
  .form-left-to-w3l input[type="password"],
  .form-left-to-w3l input[type="text"] {
    font-size: 16px;
    padding: 1em 1em;
    margin: 0px 0px 30px;
  }
  .art-right-w3ls {
    padding: 19em 24em 8em 4em;
  }
  .art-right-w3ls h2 {
    font-size: 30px;
    margin-bottom: 48px;
  }
}

@media (max-width: 1680px) {
  .art-right-w3ls h2 {
    font-size: 28px;
  }
  .form-left-to-w3l input[type="email"],
  .form-left-to-w3l input[type="password"],
  .form-left-to-w3l input[type="text"] {
    font-size: 15px;
    margin: 0px 0px 26px;
  }
}

@media (max-width: 1600px) {
  .art-right-w3ls {
    padding: 12em 24em 8em 4em;
  }
  .form-left-to-w3l input[type="email"],
  .form-left-to-w3l input[type="password"],
  .form-left-to-w3l input[type="text"] {
    font-size: 14px;
    padding: 0.8em 1em;
    margin: 0px 0px 25px;
  }
  .margin-zero input[type="password"] {
    margin: 0px !important;
  }
  .art-right-w3ls h2 {
    font-size: 28px;
    margin-bottom: 47px;
  }
}

@media (max-width: 1440px) {
  .art-right-w3ls {
    padding: 6em 21em 8em 4em;
  }
  .art-right-w3ls h2 {
    font-size: 26px;
    margin-bottom: 45px;
  }
}

@media (max-width: 1366px) {
  button[type="submit"] {
    margin: 50px 0px 0px;
  }
}

@media (max-width: 1080px) {
  .mid-class {
    flex-direction: column;
  }
  .art-right-w3ls {
    order: 2;
    padding: 2em 27em 2em 3em;
  }
  button[type="submit"] {
    margin: 46px 0px 0px;
  }
  .art-right-w3ls h2 {
    font-size: 24px;
    margin-bottom: 41px;
  }
}

@media (max-width: 1024px) {
  .art-right-w3ls {
    padding: 3em 27em 6em 4em;
  }
  .form-left-to-w3l input[type="email"],
  .form-left-to-w3l input[type="password"],
  .form-left-to-w3l input[type="text"] {
    padding: 0.7em 0.9em;
    margin: 0px 0px 22px;
  }
}

@media (max-width: 991px) {
  button[type="submit"] {
    margin: 42px 0px 0px;
  }
}

@media (max-width: 900px) {
  .form-left-to-w3l input[type="email"],
  .form-left-to-w3l input[type="password"],
  .form-left-to-w3l input[type="text"] {
    padding: 0.7em 0.7em;
  }
  .art-right-w3ls h2 {
    margin-bottom: 36px;
  }
}

@media (max-width: 800px) {
  .art-right-w3ls {
    padding: 3em 19em 6em 4em;
  }
}

@media (max-width: 767px) {
  button[type="submit"] {
    padding: 10px 18px;
  }
}

@media (max-width: 736px) {
  .form-left-to-w3l input[type="email"],
  .form-left-to-w3l input[type="password"],
  .form-left-to-w3l input[type="text"] {
    margin: 0px 0px 20px;
  }
}

@media (max-width: 667px) {
  .art-right-w3ls {
    padding: 3em 13em 6em 3em;
  }
}
@media (max-width: 600px) {
  .art-right-w3ls {
    padding: 3em 11em 6em 3em;
  }
  .art-right-w3ls h2 {
    font-size: 23px;
  }
}

@media (max-width: 568px) {
  .form-left-to-w3l input[type="email"],
  .form-left-to-w3l input[type="password"],
  .form-left-to-w3l input[type="text"] {
    margin: 0px 0px 17px;
  }
}

@media (max-width: 480px) {
  .art-right-w3ls {
    padding: 2.5em 6em 5em 3em;
  }
  button[type="submit"] {
    margin: 37px 0px 0px;
  }
}

@media (max-width: 440px) {
  .art-right-w3ls h2 {
    margin-bottom: 31px;
  }
}

@media (max-width: 414px) {
  .art-right-w3ls {
    padding: 2.5em 4em 4em 2em;
  }
}

@media (max-width: 384px) {
}

@media (max-width: 375px) {
  .art-right-w3ls h2 {
    font-size: 21px;
  }
}

@media (max-width: 320px) {
  .art-right-w3ls {
    padding: 2.5em 2em 4em 1em;
  }
}

/*--//responsive--*/

/* 调整密码容器相对定位 */
.password-container {
  position: relative;
}

/* 优化密码切换图标样式 */
.password-toggle {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-100%); /* 调整垂直居中位置 */
  cursor: pointer;
  color: #666;
  z-index: 2;
  font-size: 18px;
  opacity: 0.7;
  transition: opacity 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 24px;
  width: 24px;
}

.password-toggle:hover {
  opacity: 1;
}

/* 调整图标大小 */
.password-toggle .el-icon {
  font-size: 18px;
  color: #666;
}
</style>
